home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Deutsche Edition 1
/
Deutsche Edition 1.iso
/
amok
/
051-060
/
amok58
/
realconversions2
/
realconversions2.doc
< prev
next >
Wrap
Text File
|
1993-11-04
|
4KB
|
127 lines
DEFINITION RealConversions2;
*********************************************************************
IMPORTENT: This module can be used for converting REAL- or
LONGREAL-numbers. This works, because the compiler allows
the conditional compiling:
Normal compiling gives a module for REAL-numbers. But if You
SET the variable LONGREAL, a module for LONGREALs results.
Call the compiler from the CLI as following to generate a
module for LONGREALs:
Oberon SET LONGREAL RealConversions2
*********************************************************************
PROCEDURE DeleteSpaces(VAR str:ARRAY OF CHAR);
PROCEDURE StringToReal(str:ARRAY OF CHAR;
VAR x:Real):BOOLEAN;
PROCEDURE RealToString(x:Real;
VAR str:ARRAY OF CHAR;
gs,nks:INTEGER;expo,left:BOOLEAN):BOOLEAN;
The procedure StringToReal() converts a string to a Real-number.
RealToString() converts a Real-number to a string.
The advantage of my procedures are, that the conversion is more
accurate than that of the original module OberonV2.0.RealConversions.
Furthermore i like the format of my RealToString() better.
PROCEDURE StringToReal(str:ARRAY OF CHAR; VAR x:Real):BOOLEAN;
This is the procedure for converting a character-string to a
real-number. The result is TRUE if the syntax of the string is as
following:
digit = "0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9".
sign = ["+"|"-"].
int = {digit}.
real = sign int ["." int] [("E"|"e") sign int].
The string MUST NOT contain any spaces. If You want to use StringToReal()
with strings that contain spaces, You have to call DeleteSpaces() before
You call StringToReal().
If the string contains a number which is greater as the REAL- or
LONGREAL-range, the procedure returns FALSE.
Examples:
String Real-Number Result
"" 0.0 TRUE
"1" 1.0 TRUE
"3.84" 3.84 TRUE
"-.34532" -0.34532 TRUE
"23." 23.0 TRUE
".E" 0.0 TRUE
"-34.5E+12" -3.45E13 TRUE
"3,4" --- FALSE
"--3" --- FALSE
"3.45.E3" --- FALSE
"9.8E372" --- FALSE
PROCEDURE RealToString(x:Real;
VAR str:ARRAY OF CHAR;
gs,nks:INTEGER;expo,left:BOOLEAN):BOOLEAN;
RealToString() converts x into a character-string. gs determines, how
much significant digits the number should have. The following numbers
have five significant digits:
12345
123.45
1.2345E-13
0.000000054321
The leading zeros of the last number are not significant digits
because 0.000000054321 = 5.4321E-8. The factor E-8 is only a result of
the used unit of measurement and does not affect the accuracy of the
number.
0.012 m =12E-3 m = 12 mm
The above lengths in meters or millimeters are equivalent, the
accuracy is allways two digigs or 1/100 = 1%.
Sometimes it is usefull to restrict the number of digits behind the
point. For example, if You calculate a price in DM or $, it is not
usefull to get a result like 2.89546. With nks You can restrict the
digits behind the decimalpoint. If nks=n, then the number of digits
behind the point is allway equal or less than n. If x has more digits
before the point than gs allows, the scientific notation is used. ( in
this case 3.4E7 means 3.4 * 10^7 ) This notation is allways used, if
expo=TRUE. If left=TRUE, the number is insertet at the left side of
the string, otherwise at the right side. The number is allways
roundet:
123.453 ==> 123.45
123.456 ==> 123.46
-34.9973 ==> -35.00
IF |x|< 1, then the number of significant digits is less than gs. The
reason for this is that most people like 0.000 better than
7.894E-11. To get the correct value 7.894E-11, You can call
RealToString() as following:
ok:=RealToString(number,str,4,4, ABS(x)<=1, left);
IF |x|<=1, the scintific notation is used.
The result of RealToSting() is TRUE, if the string is large
enough to contain the number.
Stefan Salewski, August 14, 1991